package com.jnshu.task.annotion;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import com.jnshu.task.annotion.Mapper;
import com.jnshu.task.app.User;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class AnnotationMybatis {
    public static SqlSession getSqlSession() {
        String resource = "src/main/resources/sqlMapConfig1.xml";
        InputStream is = null;
        try {
            is = new FileInputStream(resource);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return sessionFactory.openSession();
    }
    public static void main(String[] args) {
        SqlSession sqlSession = getSqlSession();
        //得到UserMapperI接口的实现类对象，UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        //执行查询操作，将查询结果自动封装成User返回
        User user=mapper.findUserById1(1);
        //使用SqlSession执行完SQL之后需要关闭SqlSession
        sqlSession.close();
        System.out.println(user);


    }
}